﻿'
' Boogaloo® - http://www.mindfly.com
' Copyright (c) 2008
' by Mindfly, Inc.
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
' INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
' DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
' SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
' SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
' WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
' THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Imports System.Xml
Imports System.Xml.XPath
Imports System.Web
Imports System.Net
Imports System.IO
Imports System.Text

Namespace Rss

    Public Class RssFeed
        Inherits RssBase

#Region "Constructors"

        Public Sub New()

        End Sub

        Public Sub New(ByVal feedDoc As XmlDocument, Optional ByVal max_items As Integer = 0)


            If feedDoc IsNot Nothing Then
                channel = RssChannel.GetRssChannel(feedDoc.DocumentElement, max_items)
            End If


        End Sub

        Public Sub New(ByVal title As String, ByVal link As Uri, ByVal description As String)
            channel = New RssChannel(title, link, description)
        End Sub

        Public Function ToXmlString() As String
            If channel Is Nothing Then Throw New ArgumentNullException("channel")

            Dim sb As New StringBuilder

            sb.Append("<?xml version=""1.0"" encoding=""utf-8""?>")
            sb.Append("<rss version=""2.0""")

            Dim nslist As List(Of RssExtensionNamespace) = RssExtensionNamespace.GetExtensionNamespaces(channel)
            If nslist.Count > 0 Then
                Dim ns As RssExtensionNamespace = Nothing
                For Each ns In nslist
                    sb.Append(String.Format(" xmlns:{0}=""{1}""", ns.abbr, ns.namespace_uri.ToString))
                Next
            End If
            sb.Append(">") ' end of the <rss>
            sb.Append(channel.ToXmlString)
            sb.Append("</rss>")

            Return sb.ToString

        End Function

#End Region


        ''' <summary>
        ''' Returns RssFeed based on feed from a Uri and caches based on parameters passed. cacheDuration is an integer value representing the number of minutes to cache a feed.
        ''' </summary>
        Public Shared Function Load(ByVal source As Uri, Optional ByVal max_items As Integer = 0, Optional ByVal cacheData As Boolean = True, Optional ByVal cacheDuration As Integer = 30, Optional ByVal useragent As String = "") As RssFeed
            Dim key As String = String.Format("RssFeed_{0}", source.ToString)
            Dim feedXml As String = String.Empty
            Dim feed As RssFeed = Nothing


            If RssBase.Cache(key) IsNot Nothing And cacheData Then

                feed = CType(RssBase.Cache(key), RssFeed)

            Else
                If source IsNot Nothing Then

                    Dim response As HttpWebResponse = SyndicationEncodingUtility.CreateHttpWebResponse(source, useragent)

                    If response IsNot Nothing Then

                        Using sr As New StreamReader(response.GetResponseStream())
                            feedXml = sr.ReadToEnd
                        End Using

                        Dim feedDoc As XmlDocument = Nothing
                        feedDoc = SyndicationEncodingUtility.CreateSafeDocument(feedXml)

                        If feedDoc IsNot Nothing Then

                            Try
                                feed = New RssFeed(feedDoc, max_items)
                                If cacheData Then RssBase.CacheData(key, feed, cacheDuration)
                            Catch ex As Exception

                            End Try

                        End If

                    End If

                End If

            End If

            Return feed
        End Function




#Region "Properties"

        ''' <summary>
        ''' Channel information for the feed
        ''' </summary>
        Private _channel As RssChannel
        Public Property channel() As RssChannel
            Get
                Return _channel
            End Get
            Set(ByVal value As RssChannel)
                _channel = value
            End Set
        End Property




#End Region

    End Class


End Namespace